package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ItemShelfAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BACKShelfBean;
import com.glory.wxelectricitypda.bean.ItemShelfEntity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;


import butterknife.BindView;

public class ItemShelfActivity extends BaseDialogActivity {

    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etLocationID)
    FreeUI_ClearEditText etLocationID;
    @BindView(R.id.rvRelocationInfoList)
    RecyclerView rvRelocationInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llLotInfo)
    LinearLayout llLotInfo;
    @BindView(R.id.tvLocationID)
    TextView tvLocationID;

    private ItemShelfAdapter mItemShelfAdapter;
    private String locationID = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_itemshelf;
    }

    @Override
    protected void initView() {
        rvRelocationInfoList.setLayoutManager(new LinearLayoutManager(ItemShelfActivity.this));
        rvRelocationInfoList.setItemAnimator(new DefaultItemAnimator());
        rvRelocationInfoList.addItemDecoration(new DividerItemDecoration(ItemShelfActivity.this, 1));
        mItemShelfAdapter = new ItemShelfAdapter(null);
        mItemShelfAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mItemShelfAdapter.isFirstOnly(true);
        rvRelocationInfoList.setAdapter(mItemShelfAdapter);

    }

    @Override
    protected void initData() {

        initEdit(etLotID, s -> {
            itemShelfQuery();
        });

        initEdit(etLocationID,s ->{

            if (etLotID.getText().toString().equals("")){
                showToast("请扫描批次号",false);
                return;
            }
            if (locationID.equals("") && etLocationID.getText().toString().equals("")){
                showToast("请扫描货位号",false);
                return;
            }

            if (!locationID.equals("") && !etLocationID.getText().toString().equals(locationID)){
                showToast("货位号不一致请重新扫描",false);
                return;
            }

            submin();
        });

//        freeUI_titleView.setRightListener("上架", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (etLotID.getText().toString().equals("")){
//                    showToast("请扫描批次号",false);
//                    return;
//                }
//                if (locationID.equals("") && etLocationID.getText().toString().equals("")){
//                    showToast("请扫描货位号",false);
//                    return;
//                }
//
//                if (!locationID.equals("") && !etLocationID.getText().toString().equals(locationID)){
//                    showToast("货位号不一致请重新扫描",false);
//                    return;
//                }
//
//                submin();
//            }
//        });
    }

    private void itemShelfQuery() {
        JSONObject map = new JSONObject();
        JSONObject lotid = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "itemOffShelfQuery");
            map.put("LOCATIONID", etLocationID.getText().toString());
            lotid.put("LOTID",etLotID.getText().toString());
            map.put("PARENTLOT",lotid);
//            ArrayList<MLotBean> lotBeans = new ArrayList<>();
//            MLotBean mLotBean = new MLotBean();
//            mLotBean.setLOTID(etLotID.getText().toString());
//            lotBeans.add(mLotBean);
//            JSONArray jsonArray = new JSONArray(new Gson().toJson(lotBeans));
//            map.put("LOTLIST", jsonArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.TESTBANK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BACKShelfBean>>(
                new TypeToken<ResponseBean<BACKShelfBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BACKShelfBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    locationID = response.body().getBody().getLOCATIONID();
                    tvLocationID.setText(locationID);
                    if (response.body().getBody().getTEST_LOTS().size()!=0){
                        mItemShelfAdapter.setNewData(response.body().getBody().getTEST_LOTS());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<BACKShelfBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.TESTBANK");
            }
        });
    }

    private void submin() {

        JSONObject map = new JSONObject();
        JSONObject lotid = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "itemShelf");
            map.put("LOCATIONID", etLocationID.getText().toString());
            lotid.put("LOTID",etLotID.getText().toString());
            map.put("PARENTLOT",lotid);
//            ArrayList<MLotBean> lotBeans = new ArrayList<>();
//            MLotBean mLotBean = new MLotBean();
//            mLotBean.setLOTID(etLotID.getText().toString());
//            lotBeans.add(mLotBean);
//            JSONArray jsonArray = new JSONArray(new Gson().toJson(lotBeans));
//            map.put("LOTLIST", jsonArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.TESTBANK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<ItemShelfEntity>>(
                new TypeToken<ResponseBean<ItemShelfEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ItemShelfEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("上架成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<ItemShelfEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.TESTBANK");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("BANK上架");
        return new FreeUI_AddViewEntity("header", llMain);
    }
}

